package com.shibo.generator.temUtils;

import com.shibo.generator.propConfig.TemplateConfig;
import com.shibo.generator.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/*
* 模板公共类
* */
@Component
public class TemCommon {

    //模板配置
    @Autowired
    private TemplateConfig templateConfig;

    /**
     * 通用的替换
     */
    public Map<String, Object> replaceMap(String tableName, String classDescription) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("classDescription", classDescription);
        map.put("author", templateConfig.getAuthor());
        map.put("datatime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        map.put("className", getClassName(tableName));
        //map.put("primaryKey", templateConfig.getPrimaryKeyDataType(tableName));
        return map;
    }

    /*
    * 获取类名
    * */
    public String getClassName(String tableName) {
        if (tableName.contains(templateConfig.getTablePrefix())) {
            tableName = tableName.replace(templateConfig.getTablePrefix(),"");
        }
        return tableName.substring(0,1).toUpperCase()+tableName.substring(1);
    }

    /*
    * 返回文件名
    * */
    public String returnFileName(String type) {
        String path = templateConfig.getEntityTemPath();
        switch (type) {
            case Constant.entity :
                path = templateConfig.getEntityTemPath();
                break;
            case Constant.createTable:
                path =  templateConfig.getCreateTableTemPath();
                break;
            case Constant.service :
                path =  templateConfig.getServiceTemPath();
                break;
            case Constant.mapper:
                path =  templateConfig.getMapperTemPath();
                break;
        }
        return  templateConfig.getTemplateBasePath() + path;
    }

    /*
     * 返回模板base地址
     * */
    public String getTemplateBasePath() {
        return  templateConfig.getTemplateBasePath();
    }

    /*
    * 返回包名
    * */
    public String getPackagePath(String type) {
        return templateConfig.getPackgePath() + type;
    }

    /*
     * 返回生成文件名
     * */
    public String getWriteFileBasePath() {
        return templateConfig.getWriteFileBasePath();
    }



    /**
     * 首字母大写改成小写
     * 格式化name User->user UserRole->userRole
     */
    public String formatNameLowerCase(String ColumnName) {
        String[] arr = ColumnName.split("_");
        ColumnName = "";
        ColumnName += arr[0].substring(0, 1).toLowerCase() + arr[0].substring(1);
        for (int i = 1; i < arr.length; i++) {
            ColumnName += arr[i].substring(0, 1).toUpperCase() + arr[i].substring(1);
        }
        return ColumnName;
    }
}
